package com.rpgfw.graphics;

import com.rpgfw.scene.Scene;
import test.Main;

import javax.swing.*;
import java.awt.*;

/*
* 游戏的主面板，用于显示scene和entity
* */
public class GamePanel extends JPanel{
    Scene scene=null;
    Dimension grid=new Dimension(10,10);
    PixelGraphics graphics;
    Point displayStart=new Point(0,0);

    boolean gridLineVisible=false;
//    /*
//    * 设置格子长宽（像素）
//    * */
//    public GamePanel(Dimension gridSize){
//
//    }
//    /*
//    * 使用已有的Scene对象初始化gamepanel
//    * */
//    public GamePanel(Scene scene){
//
//    }
    public GamePanel(Scene scene,Dimension gridSize){
        init(scene, gridSize);
    }

    void init(Scene scene,Dimension gridSize){
        this.scene=scene;
        this.grid=gridSize;
        this.setLayout(null);
    }

    /*显示起点开始的scene图像*/
    /*public void display(int x,int y){
        this.displayStart.setLocation(x,y);
        display();
    }*/
    public void display(){

        this.repaint();
        //System.out.println("display");
    }
    public void setScene(Scene scene){
        this.scene=scene;
    }

    public void setGridSize(Dimension gridSize){
        this.grid.setSize(gridSize);
    }
    public void paint(Graphics g){
        g.setColor(scene.getInitCl());
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        /*for(int i=displayStart.y;i<scene.getHeight();i++){
            for(int j=displayStart.x;j<scene.getWidth();j++){
                g.setColor(scene.getPixel(j,i));
                g.fillRect((j-displayStart.x)*this.grid.width,(i-displayStart.y)*this.grid.height
                        ,this.grid.width,this.grid.height);
            }
        }*/
        //Main.count=0;
        //Main.repaintcount++;
        //System.out.println(1111);
        for(int i=0;i<scene.getHeight();i++){
            for(int j=0;j<scene.getWidth();j++){
                if(i>=0&&j>=0){
                    g.setColor(scene.getPixel(j,i));
                    g.fillRect((j+scene.getX())*(int)this.grid.getWidth(),(i+scene.getY())*(int)this.grid.getHeight(),(int)this.grid.getWidth(),(int)this.grid.getHeight());
                    //System.out.println(this.grid.getWidth()+" "+Main.repaintcount+":"+Main.count++);
                    //System.out.print(scene.getPixel(j,i).getRGB()==-4473925?"":"1111111");
                }
            }
        }
    }

}
